package com.auth.Sercurity.demoList;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hujian
 * @version 1.0
 * @description: 工资涨幅给谁带来的影响
 *     观察者模式
 * @date 2022/2/18 9:13
 */
public class ObjectDemo2 {

    public static void main(String[] args) {
        Money money=new Change();
        MoneyChange wife = new Wife();
        MoneyChange baby = new Baby();
        money.add(wife);
        money.add(baby);
        money.change(1000);
        money.remove(baby);
        money.change(-1000);
    }


    //    抽象目标
    abstract static class Money{
        //        工资涨幅给谁带来的影响
        protected List<MoneyChange> list=new ArrayList<>();

        void add(MoneyChange moneyChange){
            list.add(moneyChange);
        }
        void remove(MoneyChange moneyChange){
            list.remove(moneyChange);
        }
        abstract void change(Integer num);
    }
    //    具体目标
    static class Change extends Money{

        @Override
        void change(Integer num) {
            for (MoneyChange moneyChange : list) {
                moneyChange.change(num);
            }
        }
    }
    //    抽象对象
    interface MoneyChange{
        void change(Integer num);
    }
    //    具体对象
    static class Wife implements MoneyChange{

        @Override
        public void change(Integer num) {
            if (num>0){
//                todo
                System.out.println("工资上涨，妻子可以买更好的化妆品");
            }else {
                System.out.println("工资下跌，妻子好像很不开心");
            }
        }
    }
    static class Baby implements MoneyChange{

        @Override
        public void change(Integer num) {
            if (num>0){
                System.out.println("工资上涨，宝宝可以买更多的玩具");
            }else {
                System.out.println("工资下跌，宝宝只能在家呆在，不能出去玩");
            }
        }
    }
}
